﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScreenRuler
{
    class FormDragger
    {
        enum DragMode
        {
            None,
            ButtonHolding,
            Dragging
        }

        public FormDragger(System.Windows.Forms.Form TargetForm)
        {
            TargetForm.MouseDown += new System.Windows.Forms.MouseEventHandler(TargetForm_MouseDown);
            TargetForm.MouseLeave += new EventHandler(TargetForm_MouseLeave);
            TargetForm.MouseMove += new System.Windows.Forms.MouseEventHandler(TargetForm_MouseMove);
            TargetForm.MouseUp += new System.Windows.Forms.MouseEventHandler(TargetForm_MouseUp);
            TargetForm.KeyDown += new System.Windows.Forms.KeyEventHandler(TargetForm_KeyDown);
            this.TargetForm = TargetForm;
        }

        void TargetForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.Modifiers == System.Windows.Forms.Keys.Control)
            {
                switch (e.KeyCode)
                {
                    case System.Windows.Forms.Keys.Up:
                        if (this.TargetForm.Height > 10)
                        {
                            this.TargetForm.Height -= 10;                            
                        }
                        break;
                    case System.Windows.Forms.Keys.Down:
                        this.TargetForm.Height += 10;
                        break;
                    case System.Windows.Forms.Keys.Left:
                        if (this.TargetForm.Width > 10)
                        {
                            this.TargetForm.Width -= 10;
                        }
                        break;
                    case System.Windows.Forms.Keys.Right:
                        this.TargetForm.Width += 10;
                        break;
                    default:
                        break;
                }
            }
            else if (e.Modifiers == System.Windows.Forms.Keys.None)
            {
                switch (e.KeyCode)
                {
                    case System.Windows.Forms.Keys.Up:
                        this.TargetForm.Top -= 10;
                        break;
                    case System.Windows.Forms.Keys.Down:
                        this.TargetForm.Top += 10;
                        break;
                    case System.Windows.Forms.Keys.Left:
                        this.TargetForm.Left -= 10;
                        break;
                    case System.Windows.Forms.Keys.Right:
                        this.TargetForm.Left += 10;
                        break;
                    default:
                        break;
                }
            }
        }

        DragMode Mode = DragMode.None;
        System.Drawing.Point DragStartMousePositon;
        System.Drawing.Point DragStartFormPosition;
        System.Windows.Forms.Form TargetForm;

        void TargetForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.Mode != DragMode.None && e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Mode = DragMode.None;                
            }
        }

        void TargetForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.Mode == DragMode.ButtonHolding)
            {
                this.Mode = DragMode.Dragging;
            }
            if (this.Mode == DragMode.Dragging &&
                ((e.Button & System.Windows.Forms.MouseButtons.Left) != System.Windows.Forms.MouseButtons.None))
            {
                System.Drawing.Point CursorPostion = System.Windows.Forms.Cursor.Position;
                this.TargetForm.Location = new System.Drawing.Point(
                    this.DragStartFormPosition.X + (CursorPostion.X - this.DragStartMousePositon.X),
                    this.DragStartFormPosition.Y + (CursorPostion.Y - this.DragStartMousePositon.Y));
            }
        }

        void TargetForm_MouseLeave(object sender, EventArgs e)
        {
            this.Mode = DragMode.None;
        }

        void TargetForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Mode = DragMode.ButtonHolding;
                this.DragStartMousePositon = System.Windows.Forms.Cursor.Position;
                this.DragStartFormPosition = this.TargetForm.Location;
            }
        }
    }
}
